package info.himanshug.www.picfun;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.SeekBar;

public class ImageView extends SurfaceView implements SurfaceHolder.Callback,
		SharedPreferences.OnSharedPreferenceChangeListener ,
		SeekBar.OnSeekBarChangeListener {

//	private Bitmap bMap;
	private Context context;
//	private SurfaceHolder holder;
	
	ImageViewRenderThread renderThread;
	
	public ImageView(Context context, AttributeSet attrs) {
		super(context, attrs);

		this.context = context;
		// register our interest in hearing about changes to our surface
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		
		renderThread = new ImageViewRenderThread(holder, context);
		renderThread.setResources(getResources());

//		setFocusable(true); // make sure we get key events
	}

	public void setActivity(Activity activity) {
		renderThread.setActivity(activity);
	}
	
	/**
	 * Standard override to get key-press events.
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		return true;
	}

	/**
	 * Standard override for key-up. We actually care about these, so we can
	 * turn off the engine or stop rotating.
	 */
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent msg) {
		return true;
	}

	/**
	 * Standard window-focus override. Notice focus lost so we can pause on
	 * focus lost. e.g. user switches to take a call.
	 */
	@Override
	public void onWindowFocusChanged(boolean hasWindowFocus) {
		// if (!hasWindowFocus) Model.getModel().pause();
	}

	/**
	 * Installs a pointer to the text view used for messages.
	 */
	// public void setTextView(TextView textView) {
	// mStatusText = textView;
	// }
	/* Callback invoked when the surface dimensions change. */
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		renderThread.setSurfaceSize(width, height);
	}

	/*
	 * Callback invoked when the Surface has been created and is ready to be
	 * used.
	 */
	public void surfaceCreated(SurfaceHolder holder) {
		if (renderThread.getState() == Thread.State.NEW) {
			renderThread.setRunning(true);
			renderThread.start();
		} else if (renderThread.getState() == Thread.State.TERMINATED) {
			renderThread = new ImageViewRenderThread(renderThread);
			renderThread.setRunning(true);
			renderThread.start();
		}
	}

	/*
	 * Callback invoked when the Surface has been destroyed and must no longer
	 * be touched. WARNING: after this method returns, the Surface/Canvas must
	 * never be touched again!
	 */
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		renderThread.setRunning(false);
		while (retry) {
			try {
				renderThread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}
	}

	public void openImage(String imageFilePath, int radius) {
		renderThread.mCanvasHeight = this.getHeight();
		renderThread.mCanvasWidth = this.getWidth();
		renderThread.openImage(imageFilePath, radius);
		this.invalidate();
	}
	
	public void save(boolean shouldShare) {
		renderThread.save(shouldShare);
	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		//show the progress bar
		//final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
		
		// TODO Auto-generated method stub
		int progress = seekBar.getProgress();
		Log.d(Constants.APP_NAME,"on stop tracking touch is called with progress = " + progress);
		
		renderThread.updateImage(progress);
		
		//pd.dismiss();
	}
	
	public ImageViewRenderThread getRenderThread() {
		return renderThread;
	}
}
